package com.cws.project.cws.teacher.service.impl;

import com.cws.common.constant.UserConstants;
import com.cws.framework.shiro.service.PasswordService;
import com.cws.project.cws.clazz.domain.Clazz;
import com.cws.project.cws.clazz.domain.ClazzUser;
import com.cws.project.cws.clazz.mapper.ClazzMapper;
import com.cws.project.cws.clazz.mapper.ClazzUserMapper;
import com.cws.project.cws.student.domain.Student;
import com.cws.project.cws.teacher.domain.Teacher;
import com.cws.project.cws.teacher.mapper.TeacherMapper;
import com.cws.project.cws.teacher.service.ITeacherService;
import com.cws.project.system.user.domain.UserRole;
import com.cws.project.system.user.mapper.UserMapper;
import com.cws.project.system.user.mapper.UserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author XYL
 * @date 2021-12-26 13:43
 */
@Service
public class TeacherServiceImpl implements ITeacherService {

    private static Long TEACHER_ROLE_ID = 3L;

    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordService passwordService;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private ClazzUserMapper clazzUserMapper;
    @Autowired
    private ClazzMapper clazzMapper;


    @Override
    public List<Teacher> selectTeacherList(Teacher tea) {
        List<Teacher> teachers = teacherMapper.selectTeacherList(tea);
        for (Teacher teacher : teachers) {
            voTeacher(teacher);
        }
        return teachers;
    }

    @Override
    @Transactional
    public int insertTeacher(Teacher tea) {
        //插入教师基本信息
        tea.setUserType(UserConstants.REGISTER_USER_TYPE);
        tea.randomSalt();
        tea.setPassword(passwordService.encryptPassword(tea.getLoginName(), tea.getPassword(), tea.getSalt()));
        int rows= userMapper.insertUser(tea);
        //插入角色
        insertUserRole(tea.getUserId(), TEACHER_ROLE_ID);
        //插入班级关联
        insertClazzUser(tea.getUserId(),tea.getClazzIds());
        return rows;
    }

    @Override
    public Teacher selectUserById(Long userId) {
        Teacher teacher = teacherMapper.selectUserById(userId);
        voTeacher(teacher);
        return teacher;
    }

    @Override
    public int updateTeacher(Teacher tea) {
        int rows = userMapper.updateUser(tea);
        //解除班级关联
        clazzUserMapper.deleteClazzUserByUserId(tea.getUserId());
        //重新插入
        insertClazzUser(tea.getUserId(),tea.getClazzIds());
        return rows;
    }

    /**
     * 新增用户角色信息
     */
    public void insertUserRole(Long userId, Long roleId) {
        List<UserRole> roles = new ArrayList<>();
        UserRole ur = new UserRole();
        ur.setUserId(userId);
        ur.setRoleId(roleId);
        roles.add(ur);
        userRoleMapper.batchUserRole(roles);
    }


    /**
     * 新增用户班级关联信息
     */
    public void insertClazzUser(Long userId, Long clazzId) {
        ClazzUser cu = new ClazzUser();
        cu.setUserId(userId);
        cu.setClazzId(clazzId);
        clazzUserMapper.insertClazzUser(cu);
    }
    /**
     * 新增用户班级关联信息
     */
    public void insertClazzUser(Long userId, Long[] clazzId) {
        List<ClazzUser> list= new ArrayList<>();
        for (Long clazz : clazzId) {
            ClazzUser cu = new ClazzUser();
            cu.setUserId(userId);
            cu.setClazzId(clazz);
            list.add(cu);
        }
        clazzUserMapper.insertBatchClazzUser(list);
    }

    private void voTeacher(Teacher teacher){
        ClazzUser cu = new ClazzUser();
        cu.setUserId(teacher.getUserId());
        List<ClazzUser> list = clazzUserMapper.selectClazzUserList(cu);
        List<Long> collect = list.stream().map(ClazzUser::getClazzId).collect(Collectors.toList());
        teacher.setClazzListIds(collect);
        List<Clazz> clazzList = clazzMapper.selectClazzListByIds(collect);
        teacher.setClazzName(clazzList.stream().map(Clazz::getClazzName).collect(Collectors.joining(",")));
    }
}
